package net.wy.commons.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.time.DateUtils;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 12-7-11
 * Time: 下午4:00
 * To change this template use File | Settings | File Templates.
 */
public class DateUtil extends DateUtils {
    public static final String FORMAT_LONGDATETIME = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String FORMAT_SHORTDATETIME = "yyyy-MM-dd";
    public static final String FORMAT_LONGDATETIME_COMPACT = "yyyyMMddHHmmssSSS";
    public static final String FORMAT_SHORTDATETIME_COMPACT = "yyyyMMdd";

    /**
     * 由字符串变成date类型
     *
     * @param dateString
     * @param format
     * @return
     */
    public static Date getDateFormat(String dateString, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        try {
            return simpleDateFormat.parse(dateString);
        } catch (ParseException e) {
            return null;
        }
    }

    public static final String convertDateToString(java.util.Date aDate, String pattern) {
        return aDate == null ? null : new SimpleDateFormat(pattern).format(aDate);
    }

    public static java.util.Date getDayEndTime(java.util.Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return getDayEndTime(calendar);
    }

    public static java.util.Date getDayEndTime(String date) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(convertStringToDateTime(FORMAT_SHORTDATETIME, date));
        return getDayEndTime(calendar);
    }

    public static java.util.Date getDayEndLongTime(String date) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(convertStringToDateTime("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", date));
        return calendar.getTime();
    }

    public static java.util.Date convertStringToDateTime(String datePattern, String strDateTime) throws ParseException {
        return new java.util.Date(new SimpleDateFormat(datePattern).parse(strDateTime).getTime());
    }

    public static java.util.Date getDayEndTime(Calendar calendar) {
        Calendar tmpCalendar = (Calendar) calendar.clone();
        tmpCalendar.set(11, 0);
        tmpCalendar.set(12, 0);
        tmpCalendar.set(13, 0);
        tmpCalendar.set(14, 0);

        tmpCalendar.add(5, 1);
        tmpCalendar.add(14, -1);
        return tmpCalendar.getTime();
    }

    public static java.util.Date getDayStartTime(String date) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(convertStringToDateTime(FORMAT_SHORTDATETIME, date));
        return getDayStartTime(calendar);
    }

    public static java.util.Date getDayStartLongTime(String date) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(convertStringToDateTime("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", date));
        return calendar.getTime();
    }

    public static java.util.Date getDayStartTime(Calendar calendar) {
        Calendar tmpCalendar = (Calendar) calendar.clone();
        tmpCalendar.set(11, 0);
        tmpCalendar.set(12, 0);
        tmpCalendar.set(13, 0);
        tmpCalendar.set(14, 0);
        return tmpCalendar.getTime();
    }
    
    /**
	 * 日期友好化
	 * 显示为与当前时间比较的时间间隔，如"15秒前" "昨天 15:26"
	 * @param date
	 * @return
	 */
	public static String prettifyDate(Date date){
		if(date == null){
			return null;
		}
		Calendar now = Calendar.getInstance();
		long nowTime = now.getTimeInMillis();
		long dateTime = date.getTime();
		long time = nowTime - dateTime;
		String text = null;
		if(time < 0){
			text = "刚刚";
		}else if(time < 60*1000){
			text = formatDate(new Date(time),"ss秒前");
		}else if(time < 60*60*1000){
			text = formatDate(new Date(time),"mm分钟前");
		}else if(time < 24*60*60*1000){
			text = formatDate(new Date(time-1000*60*60*8),"H小时前");
		}else{
			text = formatDate(date,"yyyy年MM月dd日");
		}
		return text;
	}
	
	/**
	 * 按正则表达式格式化日期
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String formatDate(Date date,String pattern){
		String text = null;
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		text = format.format(date);
		return text;
	}
	
	/**
	 * 取得零时零分零秒的时间点
	 * @param calendar 一个日历
	 * @param day 当day==null时为当月第一天，当day!=0时为当天减去day后的日期
	 * @param month 当month==null时为当年一月，当month!=0时为当月减去month后的日期
	 * @return
	 */
	public static Calendar getZeroHour(Calendar now, Integer day, Integer month){
		Calendar calendar = (Calendar)now.clone();
		calendar.clear(Calendar.MILLISECOND);
		calendar.clear(Calendar.SECOND);
		calendar.clear(Calendar.MINUTE);
		calendar.set(Calendar.HOUR_OF_DAY,0);
		if(day == null){
			calendar.set(Calendar.DAY_OF_MONTH, 1);
		}else if(day != 0){
			calendar.add(Calendar.DATE, day);
		}
		if(month == null){
			calendar.set(Calendar.MONTH, Calendar.JANUARY);
		}else if(month != 0){
			calendar.add(Calendar.MONTH, month);
		}
		return calendar;
	}
}
